package br.com.android.financas;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import br.com.android.financas.model.Apresentavel;
import br.com.android.financas.model.Despesa;
import br.com.android.financas.model.Poupanca;

public class Utils {

	public static Map<MesTotal, List<Apresentavel>> mesclar(
			Map<MesTotal, List<Apresentavel>> lista,
			Map<MesTotal, List<Apresentavel>> incluir) {

		Set<MesTotal> meses = incluir.keySet();

		for (MesTotal mes : meses) {

			if (lista.containsKey(mes)) {
				lista.get(mes).addAll(incluir.get(mes));
			} else {
				lista.put(mes, incluir.get(mes));
			}
		}

		meses = lista.keySet();

		for (MesTotal mes : meses) {

			double total = 0;
			double receitas = 0;

			for (Apresentavel ap : lista.get(mes)) {
				if (ap instanceof Despesa) {
					total += ap.getValor();

				} else if (ap instanceof Poupanca) {
					if (((Poupanca) ap).isDespesa())
						total += ap.getValor();
				} else
					receitas += ap.getValor();
			}

			mes.setTotal(receitas - total);

		}

		return lista;
	}

	public static Map<MesTotal, List<Apresentavel>> mesclar(List<MesTotal> incluir) {

		Map<MesTotal, Double> tmp=new HashMap<MesTotal, Double>();

		for(MesTotal mes: incluir){
			if(!tmp.containsKey(mes))
				tmp.put(mes, mes.getTotal());
			else{
				mes.adicionarAoTotal(tmp.get(mes));
				tmp.put(mes, mes.getTotal());
			}
		}
		Map<MesTotal, List<Apresentavel>> resp=new HashMap<MesTotal, List<Apresentavel>>();

		Set<MesTotal> meses=tmp.keySet();

		for(MesTotal mes: meses){
			resp.put(mes, new ArrayList<Apresentavel>());
		}
		return resp;
	}

	public static void copyFile(File source, File dest) throws IOException {
		FileInputStream fis = new FileInputStream(source);

		OutputStream output = new FileOutputStream(dest);
		// Transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = fis.read(buffer)) > 0) {
			output.write(buffer, 0, length);
		}

		// Close the streams
		output.flush();
		output.close();
		fis.close();
	}

	public static void copyFile(File source, File dest, BackUp bk)
			throws IOException {
		FileInputStream fis = new FileInputStream(source);
		Long size = fis.getChannel().size();
		OutputStream output = new FileOutputStream(dest);
		// Transfer bytes from the inputfile to the outputfile
		byte[] buffer = new byte[1024];
		int length;
		while ((length = fis.read(buffer)) > 0) {
			output.write(buffer, 0, length);
			bk.setProgress(length, size);
		}

		// Close the streams
		output.flush();
		output.close();
		fis.close();

	}

	public static double getTotal(List<? extends Apresentavel> lista) {
		double total = 0;
		for (Apresentavel apresentavel : lista) {
			total += apresentavel.getValor();
		}
		return total;
	}

	public static boolean isDebugglabe(Context context) {
		return (0 != (context.getApplicationInfo().flags &= ApplicationInfo.FLAG_DEBUGGABLE));
	}

}
